import QtQuick 1.1
//import com.nokia.symbian 1.1
import com.nokia.meego 1.0
import Qt.labs.folderlistmodel 1.0
import "platform.js" as PLATFORM

Page {
    id: fileDialog

    property alias folder: folderModel.folder

    signal accepted(string fileName)

    Rectangle {
        anchors.fill: parent
        color: visual.colorBackgrond
    }

    Component {
        id: fileListDelegate

        FileListItem {
            titleText: fileName
            isFolder: folderModel.isFolder(index)

            onClicked: {
                if (folderModel.isFolder(index)) {
                    folderModel.folder = filePath;
                    fileList.currentIndex = -1;
                } else {
                    pageStack.pop();
                    accepted(filePath)
                }
            }
        }
    }

    FolderListModel {
        id: folderModel
        nameFilters: ["*.db"]
        folder: coinModel.fileName
        sortField: FolderListModel.Name

        onFolderChanged: {
            statusBar.text = String(folder).split('/').pop()
        }
    }

    ListView {
        id: fileList
        width: parent.width
        anchors.top: PLATFORM.TYPE === "harmattan" ? viewHeader.bottom : parent.top
        anchors.bottom: parent.bottom
        currentIndex: -1
        delegate: fileListDelegate
        model: folderModel
        highlightMoveSpeed: -1
    }

    ViewHeader {
        id: viewHeader
        clickable: false
        visible: PLATFORM.TYPE === "harmattan"
        titleText: new String(folderModel.folder).split('/').pop()
        subtitleText: new String(folderModel.folder).replace(/.*?:\/\//g, "")
    }

    tools: ToolBarLayout {
        ToolIcon {
            platformIconId: "toolbar-back"
            onClicked: pageStack.pop()
        }
        ToolIcon {
            platformIconId: PLATFORM.TYPE == "symbian" ? "toolbar-previous" : "toolbar-undo"
            onClicked: folderModel.folder = folderModel.parentFolder
        }
    }
}
